- 查询surefire-plugins的资料的时候不好找,故整理
- 方便查询+review其使用方法
Documents
- surefire官网
- http://maven.apache.org/surefire/maven-surefire-plugin/
- 里面的说明比较简单,可参考的有
- 基本用法
- git库地址
- FAQ有些价值
- examples中有些有用的场景
-这个参见下面的normal case分析
- options说明,跟下面的configuration相似
normal case
- 主要参考官网场景,结合使用心得分析
Using TestNG
Using JUnit
- http://maven.apache.org/components/surefire/maven-surefire-plugin/examples/junit.html
- upgrade check for junit4.x
- junit版本导致可运行的tests不同,surefire2.7+可检查
- parallel running tests
- 提高效率神器
- junit categories
- 可结合groups实现分组运行测试用例
Using Pojo Tests
Skipping Tests
Skip after failure
- http://maven.apache.org/components/surefire/maven-surefire-plugin/examples/skip-after-failure.html
- 回归测试可用,或类似要求tests全部通过或一定百分比通过的
- 但对版本有要求
- 对reuseFork值有要求
- surefire-v2.19.1之后可以配合rerunFailingTestsCount使用
Inclusions and exclusions of Tests
http://maven.apache.org/components/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html
"**/Test*.java" - includes all of its subdirectories and all Java filenames that start with "Test". "**/*Test.java" - includes all of its subdirectories and all Java filenames that end with "Test". "**/*TestCase.java" - includes all of its subdirectories and all Java filenames that end with "TestCase".
支持正则
- 多个用逗号分割
- **是多路径
- *是多字符
- ?是单字符
Running a single test
- http://maven.apache.org/components/surefire/maven-surefire-plugin/examples/single-test.html
能指定单个或多个class
mvn -Dtest=TestSquare,TestCi*le test
能指定单个或多个method
mvn -Dtest=TestCircle#testOne+testTwo test
re-run failing test
- http://maven.apache.org/components/surefire/maven-surefire-plugin/examples/rerun-failing-tests.html
- 顾名思义
class loading and forking
- 待补充
debugging tests
- 待补充
using system properties
- 待补充
configuring the classpath
- 待补充
selecting providers
- 待补充
new error summary
- 待补充
fork options and parallel test excution
- http://maven.apache.org/components/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html
- 重要,要好好看下
- 待补充
using console logs
shutdown of forked JVM
FAQ
如何优化测试效率
- 资源允许情况下,配置多VM
- 配置多线程
- 调优VM参数
- 减少大资源的重复创建,如db连接池,单元测试时c3p0换成jdbc,然后单独测试c3p0即可
如何指定单个测试用例/部分测试用例
- mvn test -Dtest=
- 后面值是类的完全路径名
Perm溢出,如何处理
- 参考configuration中的argLine
- http://stackoverflow.com/questions/23260057/the-forked-vm-terminated-without-saying-properly-goodbye-vm-crash-or-system-exi
- 亦可修改MVN全局配置:
configuration可配置值列表(base on surefire-v2.19.1)
<additionalClasspathElements></additionalClasspathElements>
<argLine></argLine>
- 可用于配置jvm的相关参数
<basedir></basedir>
<childDelegation></childDelegation>
<classesDirectory></classesDirectory>
<classpathDependencyExcludes></classpathDependencyExcludes>
<classpathDependencyScopeExclude></classpathDependencyScopeExclude>
<debugForkedProcess></debugForkedProcess>
<dependenciesToScan></dependenciesToScan>
<detail></detail>
<disableXmlReport></disableXmlReport>
<enableAssertions></enableAssertions>
<environmentVariables></environmentVariables>
<excludedGroups></excludedGroups>
<excludes></excludes>
<excludesFile></excludesFile>
<failIfNoSpecifiedTests></failIfNoSpecifiedTests>
<failIfNoTests></failIfNoTests>
<forkCount></forkCount>
- Option to specify the number of VMs to fork in parallel in order to execute the tests. When terminated with "C", the number part is multiplied with the number of CPU cores. Floating point value are only accepted together with "C". If set to "0", no VM is forked and all tests are executed within the main process.
- Example values: "1.5C", "4"
- The system properties and the argLine of the forked processes may contain the place holder string ${surefire.forkNumber}, which is replaced with a fixed number for each of the parallel forks, ranging from 1 to the effective value of forkCount times the maximum number of parallel Surefire executions in maven parallel builds, i.e. the effective value of the -T command line argument of maven core.
<forkMode></forkMode>
- Option to specify the forking mode. Can be "never", "once", "always", "perthread". "none" and "pertest" are also accepted for backwards compatibility. "always" forks for each test-class. "perthread" will create threadCount parallel forks, each executing one test-class. See also parameter reuseForks.
- 现在啊建议用reuseFork代替
<forkedProcessTimeoutInSeconds></forkedProcessTimeoutInSeconds>
- Kill the forked test process after a certain number of seconds. If set to 0, wait forever for the process, never timing out.
<goal></goal>
<groups></groups>
<includes></includes>
<includesFile></includesFile>
<indentSize></indentSize>
<junitArtifactName></junitArtifactName>
<jvm></jvm>
<lineLength></lineLength>
<objectFactory></objectFactory>
<parallel></parallel>
- (TestNG provider) When you use the parallel attribute, TestNG will try to run all your test methods in separate threads, except for methods that depend on each other, which will be run in the same thread in order to respect their order of execution. <p/> (JUnit 4.7 provider) Supports values "classes"/"methods"/"both" to run in separate threads, as controlled by threadCount.
- Since version 2.16 (JUnit 4.7 provider), the value "both" is <strong>DEPRECATED</strong>. Use <strong>"classesAndMethods"</strong> instead.
- Since version 2.16 (JUnit 4.7 provider), additional vales are available "suites"/"suitesAndClasses"/"suitesAndMethods"/"classesAndMethods"/"all".
- 配合treadCount使用
<parallelOptimized></parallelOptimized>
<parallelTestsTimeoutForcedInSeconds></parallelTestsTimeoutForcedInSeconds>
<parallelTestsTimeoutInSeconds></parallelTestsTimeoutInSeconds>
<perCoreThreadCount></perCoreThreadCount>
<printSummary></printSummary>
<properties></properties>
<redirectTestOutputToFile></redirectTestOutputToFile>
<remoteRepositories></remoteRepositories>
<reportFormat></reportFormat>
<reportNameSuffix></reportNameSuffix>
<reportsDirectory></reportsDirectory>
<rerunFailingTestsCount></rerunFailingTestsCount>
<reuseForks></reuseForks>
-Indicates if forked VMs can be reused. If set to "false", a new VM is forked for each test class to be executed. If set to "true", up to forkCount VMs will be forked and then reused to execute all tests.
-配合forkCount
<runOrder></runOrder>
<shutdown></shutdown>
<skip></skip>
<skipAfterFailureCount></skipAfterFailureCount>
-出现n个测试用例失败后,会跳过剩余测试用例并终止测试
-需要JUnit4.0+,TestNG5.10+
-可参考:http://maven.apache.org/components/surefire/maven-surefire-plugin/examples/skip-after-failure.html
<skipExec></skipExec>
<skipTests></skipTests>
<suiteXmlFiles></suiteXmlFiles>
<systemProperties></systemProperties>
<systemPropertiesFile></systemPropertiesFile>
<systemPropertyVariables></systemPropertyVariables>
<test></test>
<testClassesDirectory></testClassesDirectory>
<testFailureIgnore></testFailureIgnore>
<testNGArtifactName></testNGArtifactName>
<testSourceDirectory></testSourceDirectory>
<threadCount></threadCount>
- 配合parallel使用
- (TestNG/JUnit 4.7 provider) The attribute thread-count allows you to specify how many threads should be allocated for this execution. Only makes sense to use in conjunction with the parallel parameter.
<threadCountClasses></threadCountClasses>
<threadCountMethods></threadCountMethods>
<threadCountSuites></threadCountSuites>
<trimStackTrace></trimStackTrace>
<useFile></useFile>
<useManifestOnlyJar></useManifestOnlyJar>
<useSystemClassLoader></useSystemClassLoader>
<useUnlimitedThreads></useUnlimitedThreads>
<workingDirectory></workingDirectory>